﻿// (c) Copyright 2013 Mike Riedel.
// This source is subject to the Microsoft Public License (Ms-PL).
// All other rights reserved.

namespace FreshMeat.Threading
{
   /// <summary>
   /// Specialized reader-writer lock that only allows a single reader at a time,
   /// effectively giving it the functionality of a basic lock.  This comes in 
   /// handy when code has been written to use an <see cref="IReaderWriterLock"/> 
   /// but the current circumstances don't warrant the trade-off in performance
   /// that comes with a true reader-writer lock.  For example, if there are more
   /// writers than readers then this class would likely yield better performance.
   /// </summary>
   public class SingleReaderWriterLock : IReaderWriterLock
   {
      // Use this same lock for both read and write.
      readonly Lock myLock = new Lock();

      /// <summary>
      /// Gets the lock for read access.
      /// </summary>
      /// <returns></returns>
      public ILock Read()
      {
         return myLock;
      }

      /// <summary>
      /// Gets the lock for write access.
      /// </summary>
      /// <returns></returns>
      public ILock Write()
      {
         return myLock;
      }
   }
}